
#include "skbuff.h"
#include "eth.h"
#include "ip.h"
#include "udp.h"
#include "tftp.h"
#include "utils.h"
#include "2410lib.h"

char format_time[20];
#define PORT 6911
int udp_init(void)
{
	return 0;
}

int udp_rcv_packet(struct sk_buff *skb)
{
	unsigned long client_ip;
	unsigned short client_port;
 	struct sk_buff *sendskb;
	UINT8T* senddata;
	int tmp;
	
	struct udphdr *udp_hdr = (struct udphdr *)(skb->data);
	skb->len = ntohs(udp_hdr->len);
	skb_pull(skb, sizeof(struct udphdr));
	uart_printf("receives :%s\n", (UINT8T *)(skb->data));
	
	client_ip = ip_get_source_ip(skb);
	client_port = udp_get_source_port(skb);
	
 	udp_hdr->dest = htons(client_port);
  udp_hdr->source = htons(PORT);
	tmp = strlen((UINT8T *)(skb->data));
 	udp_send_length(skb, client_ip, PORT, client_port,strlen((UINT8T *)(skb->data)));
	

	rtc_gettime(tmp);
	
 	sendskb = alloc_skb(ETH_FRAME_LEN);
 	udp_skb_reserve(sendskb);
 	senddata = (UINT8T *)skb_put(sendskb, strlen(format_time)*sizeof(UINT8T));
 	strcpy(senddata, format_time);
	uart_printf("send length:%d %d content :%s\n", strlen(senddata),strlen(format_time), senddata);
 	udp_send_length(sendskb, client_ip, PORT, client_port,strlen(senddata));
	
	return 0;
}

int udp_send_length(struct sk_buff *skb, unsigned long ip,
	unsigned short source, unsigned short dest,int length)
{
	struct udphdr *udp_hdr;

	udp_hdr = (struct udphdr *)skb_push(skb, sizeof(struct udphdr));
	udp_hdr->source = htons(source);
	udp_hdr->dest = htons(dest);
	udp_hdr->len = htons(8+length);
	udp_hdr->check = 0;

	ip_send(skb, ip, UDP);

	return 0;
}

int udp_send(struct sk_buff *skb, unsigned long ip,
	unsigned short source, unsigned short dest)
{
	struct udphdr *udp_hdr;

	udp_hdr = (struct udphdr *)skb_push(skb, sizeof(struct udphdr));
	udp_hdr->source = htons(source);
	udp_hdr->dest = htons(dest);
	udp_hdr->len = htons(12);
	udp_hdr->check = 0;

	ip_send(skb, ip, UDP);

	return 0;
}

void udp_skb_reserve(struct sk_buff *skb)
{
	ip_skb_reserve(skb);
	skb_reserve(skb, sizeof(struct udphdr));
}

unsigned short udp_get_source_port(struct sk_buff *skb)
{
	struct udphdr *udp_hdr;
	
	udp_hdr = (struct udphdr *)(skb->buf + ETH_HLEN + sizeof(struct iphdr));
	return ntohs(udp_hdr->source);
}

void rtc_gettime(int n)
{
	INT8T *day[8] = {" ","SUN","MON","TUE","WED","THR","FRI","SAT"};
  INT32T g_nYear,g_nMonth,g_nDate,g_nWeekday,g_nHour,g_nMin,g_nSec;

	rRTCCON = 0x01;

	if(rBCDYEAR == 0x99) 
			g_nYear = 0x1999;
	else 
			g_nYear = 0x2000 + rBCDYEAR;

	g_nMonth   = rBCDMON;
	g_nWeekday = rBCDDAY;
	g_nDate    = rBCDDATE;
	g_nHour    = rBCDHOUR;
	g_nMin     = rBCDMIN;
	g_nSec     = rBCDSEC;
	if(n == 1)
	{
		 sprintf(format_time,"%02x:%02x:%02x",g_nHour,g_nMin,g_nSec);
	}
	else if(n == 2)
	{
		sprintf(format_time,"%10s,  %02x/%02x/%04x",day[g_nWeekday],g_nMonth,g_nDate,g_nYear);
	}
	else if(n == 3)
	{
		 sprintf(format_time,"%02x:%02x:%02x  %10s,  %02x/%02x/%04x",g_nHour,g_nMin,g_nSec,day[g_nWeekday],g_nMonth,g_nDate,g_nYear);
	}
	else 
	{
		 strcpy(format_time, "cmd error!!");
	}
	rRTCCON = 0x0;
}


